package com.gxa.controller.zhy;

import com.gxa.common.R;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Performance;

import com.gxa.pojo.zhy.Pr;
import com.wf.captcha.SpecCaptcha;
import io.swagger.annotations.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 赵贺益
 * Date: 2021/12/02 14:31
 * Description:
 * Version: V1.0
 */
@Controller
@RequestMapping("/login")
@Api(value = "获取验证嘛和登录接口",tags = "登录界面的接口")
public class LoginController {

    @RequestMapping(value = "/queryVercode",method = RequestMethod.GET)
    @ResponseBody
    @ApiOperation(value = "获取验证码接口",notes = "获取到的就是一个对象",httpMethod = "GET", response = R.class)
    public R queryVercode(HttpServletRequest request){
        R r=new R();
        SpecCaptcha specCaptcha = new SpecCaptcha(120,40,4);
        String vercode = specCaptcha.text().toLowerCase();
        HttpSession httpSession = request.getSession();
        httpSession.setAttribute("serverVercode",vercode);
        Map map = new HashMap<>();
        map.put("image",specCaptcha.toBase64());
        System.out.println(specCaptcha.toBase64());
        r.setData(map);
        return r;
    }


    @RequestMapping(value ="verifyUserAndVercode" ,method = RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value = "登录接口",notes = "获取到的就是一个对象",httpMethod = "POST", response = ResponseData.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "登录的账号" ,required = true,paramType = "query"),
            @ApiImplicitParam(name = "user_password",value = "登录账号的密码",required =true,paramType ="query"),
            @ApiImplicitParam(name = "vercode",value = "输入的验证码",required = true,paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code=200,message = "登录成功"),
            @ApiResponse(code =401,message = "验证码错误"),
            @ApiResponse(code=501,message = "账号密码错误"),
            @ApiResponse(code = 500,message = "后台服务错误")
    })
    public  ResponseData queryUser(String username,String user_password,String vercode ,HttpServletRequest request){
        ResponseData rd=new ResponseData();
        List<Performance> list1 = new ArrayList<Performance>();//二级菜单list
        list1.add(new Performance());
        list1.add(new Performance());
        list1.add(new Performance());

        Pr pr=new Pr("系统管理",list1);
        Pr pr2=new Pr("部门管理",list1);
        Pr pr3=new Pr("员工管理",list1);
        List<Pr> list2 = new ArrayList<Pr>();
        list2.add(pr);
        list2.add(pr2);
        list2.add(pr3);

//        塞一个role_id到HttpSession
        rd.setCode(200);
        rd.setMsg("登录成功");
        rd.setToken("22222");
        rd.setData(list2);

//            r.setMsg("验证码错误");
//        if(!vercode.equalsIgnoreCase(request.getSession().getAttribute("serverVercode").toString())){
//            r.setCode(401);
//            r.setMsg("验证码错误");
//
//        }else {
//            if(r!=null){
//                System.out.println("登录成功");
//                r.setCode(200);
//            }
//
//        }
        return  rd;
    }




}
